package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.dialog.XUlPopUtil;
import com.xuexiang.xuidemo.item.XulPopupItem;
import com.xuexiang.xuidemo.provider.XulPopListProvider;
import com.xuexiang.xuidemo.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用弹出窗
 */
public class XulPopupSlice extends AbilitySlice implements Component.ClickedListener{
    private Context mContext;
    private Text text_title,text_title_bottom;
    private Image image_back;
    private Button btn_common_popup,btn_list_popup;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_xul_popup);
        inintComponent();
        initTitle();
        inintClickedListener();
    }

    private void inintComponent(){
        mContext=XulPopupSlice.this;
        image_back=(Image) findComponentById(ResourceTable.Id_image_back);
        text_title=(Text)findComponentById(ResourceTable.Id_text_title);
        text_title_bottom=(Text)findComponentById(ResourceTable.Id_text_title_bottom);
        btn_common_popup=(Button) findComponentById(ResourceTable.Id_btn_common_popup);
        btn_list_popup=(Button)findComponentById(ResourceTable.Id_btn_list_popup);

    }

    private void initTitle() {
        text_title.setText("XUlPopup");
        text_title_bottom.setText("通用弹窗");
    }
    private void inintClickedListener() {
        image_back.setClickedListener(this);
        btn_common_popup.setClickedListener(this);
        btn_list_popup.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
            case ResourceTable.Id_btn_common_popup:
                btn_common_popup.setText("隐藏普通浮层");
                XUlPopUtil.builder(getContext())
                    .setSise(600, 200)
                    .setMComponent(btn_common_popup)
                    .setMtvMsg("Popup可以设置其位置以及显示和隐藏的动画")
                    .setPopDestroyedListener(new XUlPopUtil.PopDestroyedListener() {
                        @Override
                        public void onPopDestroy() {
                            btn_common_popup.setText("显示普通浮层");
                        }
                    })
                    .show();
                break;
            case ResourceTable.Id_btn_list_popup:
                btn_list_popup.setText("隐藏列表浮层");
                XulPopListProvider xulPopListProvider = new XulPopListProvider(getContext(), getData());
                XUlPopUtil.builder(getContext()).setMComponent(btn_list_popup)
                    .setMtvMsg("Popup可以设置其位置以及显示和隐藏的动画")
                    .setMXulPopListProvider(xulPopListProvider)
                    .setIsListProvider(true)
                    .setMonItemSelectedListener(new ListContainer.ItemClickedListener() {
                        @Override
                        public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                            ToastUtil.showToast(getContext(), "Item" + (i + 1));
                        }
                    })
                    .setPopDestroyedListener(new XUlPopUtil.PopDestroyedListener() {
                        @Override
                        public void onPopDestroy() {
                            btn_list_popup.setText("显示列表浮层");
                        }
                    })
                    .setSise(400, 340)
                    .show();
                break;
        }
    }
    public List<XulPopupItem> getData() {
        ArrayList<XulPopupItem> mList = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            mList.add(new XulPopupItem("Item" + i));
        }
        return mList;
    }
}
